package net.lainiao.leetcode.other;

import java.io.File;
import java.io.FileWriter;
import java.util.Date;

//八皇后问题
public class WolfQueen {

    //女王数量
    private int max=0;

    //累计方案总数
    private int nums=0;

    //数组情况
    private int[] cols=null;

    public WolfQueen(int max){
        this.max=max;
        this.cols=new int[max];
    }

    public static void main(String[] args) {
        Date dtStart=new Date();
        WolfQueen wf=new WolfQueen(15);
        wf.eachCol(0);
        Date dtEnd=new Date();
        long mi=dtEnd.getTime()-dtStart.getTime();
        System.out.println(mi);
        AppendCount(mi);

    }

    public static void AppendCount(long mi){
        File file=new File("src/java/net/lainiao/leetcode/other/record.txt");
        System.out.println(file.getAbsoluteFile());
        try{
            FileWriter fw=new FileWriter(file,true);
            fw.write(mi+"\n");
            fw.flush();
            fw.close();
        }
        catch (Exception err){
            System.err.println(err);
        }

    }

    private void eachCol(int n) {
        boolean[] rows=new boolean[max];
        for (int i = 0; i <n ; i++) {
            rows[cols[i]]=true;
            int d=n-i;
            if(cols[i]-d>=0){
                rows[cols[i]-d]=true;
            }
            if(cols[i]+d<=max-1){
                rows[cols[i]+d]=true;
            }
        }
        for (int i = 0; i < max; i++) {
            if(rows[i]){
                continue;
            }
            cols[n]=i;
            if(n<max-1){
                eachCol(n+1);
            }
            else{
                nums++;
//                printContext();
            }
        }

        
    }
//
//    private void printContext() {
//        System.out.println("第"+nums+"种走法");
//
//        for(int i=0;i<max;i++){
//            for(int j=0;j<max;j++){
//                if(i==cols[j]){
//                    System.out.print("0 ");
//                }else
//                    System.out.print("+ ");
//            }
//            System.out.print("\n");
//        }
//    }
}